package org.dochandler.service.core;

import java.util.List;

import org.dochandler.dao.MissionReportDAO;
import org.dochandler.service.MissionReportService;
import org.dochandler.service.domain.Criteria;
import org.dochandler.service.domain.Officer;
import org.dochandler.service.domain.annotation.AllowInMissionLocationOnly;
import org.dochandler.service.domain.mission.Coordinate;
import org.dochandler.service.domain.mission.Mission;
import org.dochandler.service.domain.mission.MissionReport;
import org.esfinge.guardian.utils.ServiceLocator;

public class MissionReportServiceCore implements MissionReportService {
	private MissionReportDAO missionReportDAO;
	
	public MissionReportServiceCore() {
		setMissionReportDAO( ServiceLocator.getServiceImplementation( MissionReportDAO.class ));
	}
	
	@Override
	public Boolean isOfficerInTheMissionLocation(Officer officer, Mission mission) {
		Coordinate missionLatitude = mission.getLocation().getLatitude();
		Coordinate missionLongitude = mission.getLocation().getLongitude();
		boolean latitudeCompared = ( officer.getGlobalPosition().getLatitude().compareCoordinates( missionLatitude ) );
		boolean longitudeCompared = ( officer.getGlobalPosition().getLongitude().compareCoordinates( missionLongitude ) );
		if (latitudeCompared && longitudeCompared ) {
			return true;
		} else {
			return false;
		}
	}
	
	@Override
	public Boolean create(MissionReport document) {
		return report(document);
	}

	@Override
	public MissionReport read(Long documentId) {
		return missionReportDAO.findById(documentId);
	}

	@Override
	public Boolean delete(Long documentId) {
		return missionReportDAO.delete(documentId);
	}

	@Override
	public Boolean edit(MissionReport document) {
		return missionReportDAO.update(document);
	}

	@Override
	public List<MissionReport> listAll() {
		return missionReportDAO.findAll();
	}
	
	/**
	 * Not implemented yet
	 */
	@Override
	public List<MissionReport> listByCriteria(Criteria<?> criteria) {
		return null;
	}
	
	@Override
	@AllowInMissionLocationOnly
	public Boolean report(MissionReport missionReport) {
		return missionReportDAO.create(missionReport);
	}
	
	/*
	 * SETTERS AND GETTERS
	 */

	public MissionReportDAO getMissionReportDAO() {
		return missionReportDAO;
	}

	public void setMissionReportDAO(MissionReportDAO missionReportDAO) {
		this.missionReportDAO = missionReportDAO;
	}

}